package com.jin.webvideo.externalInterfaces.spi.videosearch;

import com.jin.webvideo.entity.Page;
import com.jin.webvideo.externalInterfaces.entity.Filmography;
import com.jin.webvideo.base.spi.Interface;
import com.jin.webvideo.base.spi.InterfaceProvider;
import com.jin.webvideo.base.spi.exception.InterfaceRunningException;
import com.jin.webvideo.util.base.StringUtil;

import java.util.List;

public abstract class SearchInterface extends Interface {
    public SearchInterface(InterfaceProvider provider) {
        super(provider);
    }
    protected abstract List<Filmography> search(String key, String[] tags,int pageNo, int pageSize) throws Exception;


    protected abstract int total(String key, String[] tags) throws Exception;

    protected Page createPage(String key , String[] tags, int pageNo, int pageSize) throws Exception {
        List<Filmography> filmographies = search(key, tags, pageNo, pageSize);
        int total = total(key, tags);
        Page page = Page.createPage(total, pageSize, pageNo);
        page.setList(filmographies);
        return page;
    }
    @Override
    protected Object run(Object... param) throws InterfaceRunningException {
        try {
            return createPage(param[0].toString(), StringUtil.split(param[1].toString(), ","), Integer.valueOf(param[2].toString()), Integer.valueOf(param[3].toString()));
        } catch (Exception e) {
            e.printStackTrace();
            throw new InterfaceRunningException(e.getCause());
        }
    }
}
